<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class ZlFortuneUp extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_fortune_up';

    protected $cacheModel = 'user.fortune_up';

    /**
     * 获取厄运消除 运势提升状态
     * @param int $rid 档案ID
     * @param int $time 日期 20201012
     * @return int 1以消除厄运 0未消除厄运
     * @throws \Exception
     */
    public function getFortuneUpStatus(int $rid, int $time): int
    {
        $cacheKey = $rid . ':' . $time;
        $isFortuneUp = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $cacheKey)->getDataAndSet(function () use ($rid, $time)
        {
            try
            {
                $isFortuneUp = $this->where(['rid' => $rid, 'time' => $time])->count();
            }
            catch (\Exception $e)
            {
                Log::write('运势提升状态获取异常：' . $e->getMessage(), 'error', true);
                $isFortuneUp = 0;
            }

            return $isFortuneUp;
        });

        return (int)$isFortuneUp;
    }

    /**
     * 档案运势提升
     * @param array $data
     * @throws \Exception
     */
    public function recodeFortuneUp(array $data)
    {
        try
        {
            $this->save($data);
        }
        catch (\Exception $e)
        {
            Log::write('运势提升数据入库异常【' . json_encode($data, JSON_UNESCAPED_UNICODE) . '】；错误消息：' . $e->getMessage(), 'error', true);
        }

        $cacheKey = $data['rid'] . ':' . $data['time'];

        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $cacheKey)->setCache(1);
    }
}
